define([
    "underscore",
    "backbone"
], function (_, Backbone) {

    var common = {
        localModel: {},
        localModelType: {
            serverTime: "",
            status: "",
            user: "model",
            gameUser: "collection",
            userList: "collection",
            itemList: "collection",
        }
    };

    common.fetchModel = function (res) {
        if (res instanceof Array) {

        } else {
            _.each(res, function (value, name) {
                switch (common.localModelType[name]) {
                    case "model": {
                        if (name in common.localModel) {
                            var model = common.localModel[name];
                            model.set(value);
                        } else {
                            var Model = Backbone.Model.extend({});
                            var model = new Model();
                            model.set(value);
                            common.localModel[name] = model;
                        }
                        localStorage.setItem(name, JSON.stringify(common.localModel[name].toJSON()));
                        break;
                    }
                    case "collection": {
                        var Model = Backbone.Model.extend({});

                        var Collection = Backbone.Collection.extend({
                            model: Model
                        });
                        var collection = new Collection(value);
                        common.localModel[name] = collection;

                        localStorage.setItem(name, JSON.stringify(common.localModel[name].toJSON()));
                        break;
                    }
                    default: {
                        common.localModel[name] = value;
                        localStorage.setItem(name, value);
                        break;
                    }
                }
            });
        }
    };

    return common;
});